package com.lysj.bhp.api.controller;

import com.lysj.bhp.api.base.ApiContent;
import com.lysj.bhp.recharge.domain.RechargeRecord;
import com.lysj.bhp.recharge.dto.RechargeRecordDto;
import com.lysj.bhp.util.random.FormatUtil;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import com.lysj.bhp.withdraw.domain.WithDraw;
import com.lysj.bhp.withdraw.dto.WithDrawDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v2/user")
@Api(value = "AccountBalanceController", tags = "充值提币记录接口")
public class ApiRechargeExtractionController extends ApiContent {

    @InitBinder
    public void InitBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, null, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

    @ApiOperation(value = "充值记录", notes = "查询条件起始时间,type,10:比特币;20:以太坊;100:BHPC;")
    @PostMapping("/recharge_list")
    public Resp rechargeList(RechargeRecordDto model) {
        //查询条件数据为空,进行初始化操作
        if (ParamUtil.isBlack(model.getLimit())) {
            model.setLimit(5);
        }
        if (ParamUtil.isBlack(model.getPage())) {
            model.setPage(0);
        }
        if (model.getBegin_createTime() == null) {
            model.setBegin_createTime(FormatUtil.addDay(new Date(), -30));
        } else {
            model.setBegin_createTime(FormatUtil.getDayStartDay(model.getBegin_createTime()));
        }
        if (model.getEnd_createTime() == null) {
            model.setEnd_createTime(FormatUtil.getDayEndDay(new Date())); //  获取到今天的结束
        } else {
            model.setEnd_createTime(FormatUtil.getDayEndDay(model.getEnd_createTime()));
        }
//        if (ParamUtil.isBlack(model.getCoinType())) {
//            model.setCoinType(10);
//        }

        //获取根据时间以及币种查询出的充值记录
        Pageable pageable = new PageRequest(model.getPage(), model.getLimit(), new Sort(Sort.Direction.DESC, "createTime"));
        model.setUserId(currentUser().getUserId());// 当前登录用户的充值记录
        Specification specification = ParamUtil.createSpecification(model);
        Page<RechargeRecord> rechargeRecordPagepage = rechargeRecordRepository.findAll(specification, pageable);
        for (RechargeRecord rechargeRecord : rechargeRecordPagepage.getContent()) {
            rechargeRecord.setPayAmount(rechargeRecord.getPayAmount().setScale(6, BigDecimal.ROUND_DOWN));
        }
        //获取最新5条充值记录
        List<RechargeRecord> rechargeRecordList = rechargeRecordRepository.findByUserIdOrderByCreateTimeDesc(currentUser().getUserId(), pageable);
        for (RechargeRecord rechargeRecord : rechargeRecordList) {
            rechargeRecord.setPayAmount(rechargeRecord.getPayAmount().setScale(6, BigDecimal.ROUND_DOWN));
        }
        Map<String, Object> respMap = new HashMap();
        respMap.put("totalpage", rechargeRecordPagepage.getTotalPages());
        respMap.put("rows", rechargeRecordPagepage.getContent());
        respMap.put("lastList", rechargeRecordList);
        return new Resp(respMap);
    }

    @ApiOperation(value = "提币记录", notes = "查询条件起始时间,type,10:比特币;20:以太坊;100:BHPC;")
    @PostMapping("/withdraw_list")
    public Resp withdrawList(WithDrawDto model) {
        //查询条件数据为空,进行初始化操作
        if (ParamUtil.isBlack(model.getLimit())) {
            model.setLimit(5);
        }
        if (ParamUtil.isBlack(model.getPage())) {
            model.setPage(0);
        }
        if (model.getBegin_createDate() == null) {
            model.setBegin_createDate(FormatUtil.addDay(new Date(), -30));
        } else {
            model.setBegin_createDate(FormatUtil.getDayStartDay(model.getBegin_createDate()));
        }
        if (model.getEnd_createDate() == null) {
            model.setEnd_createDate(FormatUtil.getDayEndDay(new Date())); //  获取到今天的结束
        } else {
            model.setEnd_createDate(FormatUtil.getDayEndDay(model.getEnd_createDate()));
        }
//        if (ParamUtil.isBlack(model.getType())) {
//            model.setType(10);
//        }

        //获取根据时间以及币种查询出的提币记录
        Pageable pageable = new PageRequest(model.getPage(), model.getLimit(), new Sort(Sort.Direction.DESC, "createDate"));
        model.setUserId(currentUser().getUserId());
        Specification specification = ParamUtil.createSpecification(model);
        Page<WithDraw> withDrawPagepage = withdrawRepository.findAll(specification, pageable);
        for (WithDraw withDraw : withDrawPagepage.getContent()) {
            withDraw.setAmount(withDraw.getAmount().setScale(6, BigDecimal.ROUND_DOWN));
        }
        //获取最新5条提币记录
        List<WithDraw> withDrawList = withdrawRepository.findByUserIdOrderByCreateDateDesc(currentUser().getUserId(), pageable);
        for (WithDraw withDraw : withDrawList) {
            withDraw.setAmount(withDraw.getAmount().setScale(6, BigDecimal.ROUND_DOWN));
        }
        Map<String, Object> respMap = new HashMap();
        respMap.put("totalpage", withDrawPagepage.getTotalPages());
        respMap.put("rows", withDrawPagepage.getContent());
        respMap.put("lastList", withDrawList);
        return new Resp(respMap);
    }
}
